<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="物流公司" prop="logisticsName">
      <el-input v-model="dataForm.logisticsName" placeholder="物流公司"></el-input>
    </el-form-item>
    <el-form-item label="快递单号" prop="logisticsOdd">
      <el-input v-model="dataForm.logisticsOdd" placeholder="快递单号"></el-input>
    </el-form-item>
    <el-form-item label="配送类型" prop="logisiticsType">
      <el-input v-model="dataForm.logisiticsType" placeholder="配送类型"></el-input>
    </el-form-item>
    <el-form-item label="配送费用" prop="logisticsPrice">
      <el-input v-model="dataForm.logisticsPrice" placeholder="配送费用"></el-input>
    </el-form-item>
    <el-form-item label="物流备注/发货单备注" prop="logisticsRemark">
      <el-input v-model="dataForm.logisticsRemark" placeholder="物流备注/发货单备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          logisticsName: '',
          logisticsOdd: '',
          logisiticsType: '',
          logisticsPrice: '',
          logisticsRemark: ''
        },
        dataRule: {
          logisticsName: [
            { required: true, message: '物流公司不能为空', trigger: 'blur' }
          ],
          logisticsOdd: [
            { required: true, message: '快递单号不能为空', trigger: 'blur' }
          ],
          logisiticsType: [
            { required: true, message: '配送类型不能为空', trigger: 'blur' }
          ],
          logisticsPrice: [
            { required: true, message: '配送费用不能为空', trigger: 'blur' }
          ],
          logisticsRemark: [
            { required: true, message: '物流备注/发货单备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/goods/logistics/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.logisticsName = data.logistics.logisticsName
                this.dataForm.logisticsOdd = data.logistics.logisticsOdd
                this.dataForm.logisiticsType = data.logistics.logisiticsType
                this.dataForm.logisticsPrice = data.logistics.logisticsPrice
                this.dataForm.logisticsRemark = data.logistics.logisticsRemark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/goods/logistics/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'logisticsName': this.dataForm.logisticsName,
                'logisticsOdd': this.dataForm.logisticsOdd,
                'logisiticsType': this.dataForm.logisiticsType,
                'logisticsPrice': this.dataForm.logisticsPrice,
                'logisticsRemark': this.dataForm.logisticsRemark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
